package com.cho3.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;


import com.blankj.utilcode.util.AdaptScreenUtils;
import com.cho3.R;
import com.cho3.adapter.MatchesAdapter;
import com.cho3.effects.transform.ScaleInTransformer;


import java.util.ArrayList;

import cn.gavinliu.android.lib.shapedimageview.ShapedImageView;

import static cn.gavinliu.android.lib.shapedimageview.ShapedImageView.SHAPE_MODE_ROUND_RECT;


public class MatchesDialog extends Dialog {
    private int imageIds[] = {R.drawable.reba, R.drawable.reba2, R.drawable.reba3, R.drawable.reba4};
    private View mContentView;
    private ViewPager mVp;
    private MatchesAdapter matchesAdapter;

    public MatchesDialog(Context context) {
        super(context, R.style.dialogStyle);
        initView(context);
    }

    private void initView(Context context) {
        mContentView = LayoutInflater.from(context).inflate(R.layout.dialog_matches, null);
        mVp = mContentView.findViewById(R.id.vp_matches);
        setContentView(mContentView);
        Window window = getWindow();
        assert window != null;
        window.setGravity(Gravity.CENTER);
        ArrayList<View> matches = new ArrayList<>();
        for (int imageId : imageIds) {
            ShapedImageView ivItem = new ShapedImageView(context);
            //ivItem.setShape(SHAPE_MODE_ROUND_RECT,15);
            ivItem.setShape(SHAPE_MODE_ROUND_RECT, AdaptScreenUtils.pt2Px(10));
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
            params.width = AdaptScreenUtils.pt2Px(100);
            params.height = AdaptScreenUtils.pt2Px(50);
            ivItem.setScaleType(ImageView.ScaleType.CENTER_CROP);
            ivItem.setMaxWidth(AdaptScreenUtils.pt2Px(150));
            // ivItem.setAdjustViewBounds(true);
            ivItem.setLayoutParams(params);
            //  ivItem.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            //ivItem.setBackground(ContextCompat.getDrawable(context,R.drawable.dialog_bgd));
            //  ShapedImageView ivItem = (ShapedImageView) LayoutInflater.from(context).inflate(R.layout.vp_item, null);
//            ShapedImageView ivItem = ivItemContainer.findViewById(R.id.iv_item);
            ivItem.setImageResource(imageId);
            //   ivItem.setScaleType(ImageView.ScaleType.CENTER_CROP);
//           // ivItem.setLayoutParams(params);
            matches.add(ivItem);
        }
        matchesAdapter = new MatchesAdapter(matches);
        mVp.setPageMargin(AdaptScreenUtils.pt2Px(AdaptScreenUtils.pt2Px(1)));
        mVp.setOffscreenPageLimit(4);
        mVp.setPageTransformer(true, new ScaleInTransformer());
        mVp.setAdapter(matchesAdapter);
        setCancelable(true);
        setCanceledOnTouchOutside(true);
    }

    @Override
    public void show() {
        super.show();
        setDialogSize();
    }

    public ViewPager getmVp() {
        return mVp;
    }

    public MatchesAdapter getMatchesAdapter() {
        return matchesAdapter;
    }

    /**
     * 设置dialog宽度
     */
    private void setDialogSize() {
        //过时
//        WindowManager.LayoutParams params = window.getAttributes();
//        params.width = (int) (getWindow().getWindowManager().getDefaultDisplay().getWidth() * 0.3);
//        window.setAttributes(params);
        Window window = getWindow();
        assert window != null;
        WindowManager.LayoutParams params = window.getAttributes();
        DisplayMetrics dm = new DisplayMetrics();
        window.getWindowManager().getDefaultDisplay().getMetrics(dm);
        params.width = (int) (dm.widthPixels * 1.0);
        window.setAttributes(params);
    }
}
